package com.yuexunit.operationman.nbsy.module.SurgicalReceipt.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.model.impl.SurgicalReceiptModel;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.presenter.ISurgicalReceiptPresenter;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.presenter.impl.SurgicalReceiptPresenter;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.ui.view.ISurgicalReceiptView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/26 0026.
 */

public class Act_SurgicalReceipt_Sign_Content extends BaseKJActivity implements ISafeOperationSignView, ISurgicalReceiptView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;

    @Bind(R.id.img_so_sign_ward)
    ImageView imgSoSignWard;
    @Bind(R.id.btn_so_sign_ward)
    TextView btnSoSignWard;
    @Bind(R.id.tv_so_sign_ward_time)
    TextView tvSoSignWardTime;
    @Bind(R.id.tv_so_sign_ward_name)
    TextView tvSoSignWardName;
    @Bind(R.id.img_so_sign_opration)
    ImageView imgSoSignOpration;
    @Bind(R.id.btn_so_sign_opration)
    TextView btnSoSignOpration;
    @Bind(R.id.tv_so_sign_opration_time)
    TextView tvSoSignOprationTime;
    @Bind(R.id.tv_so_sign_opration_name)
    TextView tvSoSignOprationName;
    @Bind(R.id.sign_01)
    TextView sign01;
    @Bind(R.id.sign_02)
    TextView sign02;

    LoginDialog loginDialog;
    LoadingDialog loadingDialog;
    private int type = -1;
    ISurgicalReceiptPresenter presenter;
    ISafeOperationSignPresenter presenter_sign;

    private int contentType = -1;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_surgicalreceipt_sign_content);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(aty);
        presenter = new SurgicalReceiptPresenter(this, new SurgicalReceiptModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        contentType = getIntent().getExtras().getInt("type");
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientName());
        if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getDiagnosis());
    }

    private void setContent() {
        switch (contentType) {
            case 1:
                sign01.setText("病房护士");
                sign02.setText("手术室护士");
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsWardNurseUrl() != null) {
                    imgSoSignWard.setVisibility(View.VISIBLE);
                    btnSoSignWard.setVisibility(View.GONE);
                    tvSoSignWardTime.setVisibility(View.VISIBLE);
                    tvSoSignWardName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsWardNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignWard);
                    tvSoSignWardTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsWardNurseTimeStr());
                    tvSoSignWardName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsWardNurse());
                }
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsOrNurseUrl() != null) {
                    imgSoSignOpration.setVisibility(View.VISIBLE);
                    btnSoSignOpration.setVisibility(View.GONE);
                    tvSoSignOprationTime.setVisibility(View.VISIBLE);
                    tvSoSignOprationName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsOrNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignOpration);
                    tvSoSignOprationTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsOrNurseTimeStr());
                    tvSoSignOprationName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBsOrNurse());
                }
                break;
            case 2:
                sign01.setText("手术室护士");
                sign02.setText("复苏室或病房护士");
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfOrNurseUrl() != null) {
                    imgSoSignWard.setVisibility(View.VISIBLE);
                    btnSoSignWard.setVisibility(View.GONE);
                    tvSoSignWardTime.setVisibility(View.VISIBLE);
                    tvSoSignWardName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfOrNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignWard);
                    tvSoSignWardTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfOrNurseTimeStr());
                    tvSoSignWardName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfOrNurse());
                }
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfRrNurseUrl() != null) {
                    imgSoSignOpration.setVisibility(View.VISIBLE);
                    btnSoSignOpration.setVisibility(View.GONE);
                    tvSoSignOprationTime.setVisibility(View.VISIBLE);
                    tvSoSignOprationName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfRrNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignOpration);
                    tvSoSignOprationTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfRrNurseTimeStr());
                    tvSoSignOprationName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getSfRrNurse());
                }
                break;
            case 3:
                sign01.setText("复苏室护士");
                sign02.setText("病房护士");
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbRrNurseUrl() != null) {
                    imgSoSignWard.setVisibility(View.VISIBLE);
                    btnSoSignWard.setVisibility(View.GONE);
                    tvSoSignWardTime.setVisibility(View.VISIBLE);
                    tvSoSignWardName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbRrNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignWard);
                    tvSoSignWardTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbRrNurseTimeStr());
                    tvSoSignWardName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbRrNurse());
                }
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbWardNurseUrl() != null) {
                    imgSoSignOpration.setVisibility(View.VISIBLE);
                    btnSoSignOpration.setVisibility(View.GONE);
                    tvSoSignOprationTime.setVisibility(View.VISIBLE);
                    tvSoSignOprationName.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbWardNurseUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgSoSignOpration);
                    tvSoSignOprationTime.setText("签名时间:" + AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbWardNurseTimeStr());
                    tvSoSignOprationName.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getFbWardNurse());
                }
                break;
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.btn_so_sign_ward, R.id.btn_so_sign_opration})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.btn_so_sign_ward:
                showTypeDialog();
                type = 1;
                break;
            case R.id.btn_so_sign_opration:
                showTypeDialog();
                type = 2;
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            switch (type) {
                case 1:
                    switch (contentType) {
                        case 1:
                            imgSoSignWard.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignWard);
                            btnSoSignWard.setVisibility(View.GONE);
                            tvSoSignWardTime.setVisibility(View.VISIBLE);
                            tvSoSignWardTime.setText("签名时间:" + str);
                            tvSoSignWardName.setVisibility(View.VISIBLE);
                            tvSoSignWardName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsWardNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsWardNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsWardNurseTimeStr(str);
                            break;
                        case 2:
                            imgSoSignWard.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignWard);
                            btnSoSignWard.setVisibility(View.GONE);
                            tvSoSignWardTime.setVisibility(View.VISIBLE);
                            tvSoSignWardTime.setText("签名时间:" + str);
                            tvSoSignWardName.setVisibility(View.VISIBLE);
                            tvSoSignWardName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfOrNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfOrNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfOrNurseTimeStr(str);
                            break;
                        case 3:
                            imgSoSignWard.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignWard);
                            btnSoSignWard.setVisibility(View.GONE);
                            tvSoSignWardTime.setVisibility(View.VISIBLE);
                            tvSoSignWardTime.setText("签名时间:" + str);
                            tvSoSignWardName.setVisibility(View.VISIBLE);
                            tvSoSignWardName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbRrNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbRrNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbRrNurseTimeStr(str);
                            break;
                    }
                    break;
                case 2:
                    switch (contentType) {
                        case 1:
                            imgSoSignOpration.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignOpration);
                            btnSoSignOpration.setVisibility(View.GONE);
                            tvSoSignOprationTime.setVisibility(View.VISIBLE);
                            tvSoSignOprationTime.setText("签名时间:" + str);
                            tvSoSignOprationName.setVisibility(View.VISIBLE);
                            tvSoSignOprationName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsOrNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsOrNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setBsOrNurseTimeStr(str);
                            break;
                        case 2:
                            imgSoSignOpration.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignOpration);
                            btnSoSignOpration.setVisibility(View.GONE);
                            tvSoSignOprationTime.setVisibility(View.VISIBLE);
                            tvSoSignOprationTime.setText("签名时间:" + str);
                            tvSoSignOprationName.setVisibility(View.VISIBLE);
                            tvSoSignOprationName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfRrNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfRrNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setSfRrNurseTimeStr(str);
                            break;
                        case 3:
                            imgSoSignOpration.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgSoSignOpration);
                            btnSoSignOpration.setVisibility(View.GONE);
                            tvSoSignOprationTime.setVisibility(View.VISIBLE);
                            tvSoSignOprationTime.setText("签名时间:" + str);
                            tvSoSignOprationName.setVisibility(View.VISIBLE);
                            tvSoSignOprationName.setText(signBean.getObject().getDoctorName());

                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbWardNurse(signBean.getObject().getDoctorName());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbWardNurseUrl(signBean.getObject().getSignPicture());
                            AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().setFbWardNurseTimeStr(str);
                            break;
                    }
                    break;
            }


            if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getHandoverId() == null) {
                Map<String, String> params = new HashMap<>();
                params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.SURGICALRECEIPTBEAN));
                presenter.insertSurgicalReceiptDate(params);
            } else {
                Map<String, String> params = new HashMap<>();
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.SURGICALRECEIPTBEAN));
                presenter.updateSurgicalReceiptDate(params);
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    @Override
    public void insertSurgicalReceiptDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateSurgicalReceiptDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

}
